<template>
  <el-form :model="ruleForm" ref="ruleForm" :label-position="labelPosition"
           :class="isEdit?'':'formItem'"
           :inline="false">

    <el-card class="box-card" shadow="never">
      <div slot="header" class="header-card">
        <span class="header-card-title">附件</span>
      </div>

      <el-row>
        <el-col>
          <el-form-item label="附件" prop="attachmentUrl" :rules="isEdit ? rules.attachmentUrl : []">
            <image-common v-model="ruleForm.attachmentUrl" :limit="20" :disabled="!isEdit" :upType="2"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-card>
  </el-form>
</template>

<script>
export default {
  name: 'attachments',//附件
  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: '',
    },
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            if (value) {
              that.handleClearValidate(key)
            }
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.state === 'write') {
        return 'top'
      } else {
        return 'right'
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool
    }
  },
  data() {

    return {
      rules: {
        attachmentUrl: [{required: true, message: "请上传附件", trigger: "blur"},]
      }
    };
  },
  created() {
  },
  mounted() {
  },
  methods: {
    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            reject();
          }
        });
      });
    },

    valid() {
      return new Promise((resolve) => {
        this.$refs.ruleForm.validate((valid) => {
          resolve(valid);
        });
      });
    },

    handleClearValidate(str) {
      this.$refs.ruleForm.clearValidate(str);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";

.elForm {
  .el-form-item {
    width: 100%;
  }
}
</style>



